home *** CD-ROM | disk | FTP | other *** search
- Path: keats.ugrad.cs.ubc.ca!not-for-mail
- From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
- Newsgroups: comp.lang.c,comp.unix.aix
- Subject: Re: Help: write() to a socket hangs unless there's a newline
- Date: 12 Mar 1996 12:44:46 -0800
- Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
- Message-ID: <4i4nnuINNgc@keats.ugrad.cs.ubc.ca>
- References: <5r68cae4wm.fsf@ritz.mordor.com>
- NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
-
- In article <5r68cae4wm.fsf@ritz.mordor.com>,
- Joseph Thomas <benjamin@ritz.mordor.com> wrote:
- >I realize this is not a direct C question, but it does have to do with
- >streams, and I tend to think the expertise to answer this is here:
- >
- >I've opened a socket, and connected it to another. I'm using write() to
- >send a message through, ex:
- >
- >write(sock_fd, "Hello world", strlen("Hello world"));
- >
- >When I do this, the message gets through to the other socket, but the
- >write() call hangs in the process that executed it.
-
- The message gets through, but what comes out at the other end is not a string.
- You forgot to include the zero terminating character by adding 1 to strlen().
-
- Then again, you never specified what kind of socket this is---i.e. whether it
- is a stream-oriented virtual circuit or a datagram socket. In a datagram
- socket, the reader can append its own zero characters to strings, by, for
- instance, reading the data into a large zero-filled buffer. Over a stream
- socket, however, if you do the above, you have no easy means of delimiting
- where one string starts and another one ends if you don't include a marker such
- as the null terminator.
-
- >However, if I send it as:
- >
- >write(sock_fd, "Hello world\n", strlen("Hello world\n"));
- >
- >It doesn't hang, but I have a newline I don't want at the other end.
-
- Sounds weird. That should not happen. Who knows what is going on? You didn't
- post the rest of your program.
-
- >In addition, for some reason, between the next write()-->read() that I
- >try, I read() two null strings before I actually get the next
- >message (this may be something particular to sockets?)
- >
- >I've tried using fflush(sock_fd), but that causes a broken pipe for
- >some reason. Can anyone say what gives here?
-
- You are confused. fflush() is a standard C library function that works
- with FILE * streams, not integer file descriptors.
-
- >Please, no advice about not using write()
-
- How about, take this to comp.unix.programmer? One is not required to know about
- socket programming in order to be a C guru. Or ask on Internet Relay Chat in
- the #unix channel.
- --
-
-